<?php
namespace Content\Controller;
use Common\Controller\AdminbaseController;
class AttributeController extends AdminbaseController 
{   
  protected $attributeObj;
  protected $typeObj;
  public function _initialize()
  {
      parent::_initialize();
      $this->attributeObj = D("Attribute");
      $this->typeObj = M("Type");
  }

	public function index()
	{
		$this->lists();
	}

	public function lists()
	{
      $status = I('get.status','null','htmlspecialchars');
      $type = I('get.type','-1','htmlspecialchars');
	    $condition = array();
        if($type>0) $condition['typeid'] = array('EQ', $type);
        if($status!='null')
            $condition['status']=$status;
        else
            $condition['status']=array('egt',0);
	      $count      =  $this->attributeObj->where($condition)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $this->attributeObj->relation('type')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show); 
        $this->display('list');
	}
  public function getAttributes($type=0)
  {
    if ($type>0) 
    {
        $condition['typeid']=$type;
        $attributeList = $this->attributeObj->where($condition)->select();
        return $attributeList;
    }else{
        return null;
    }
  }
  public function add($type=null)
  {
    if (!empty($type)) {
        $typeObj = $this->typeObj->find($type);
        $this->assign("type",$typeObj);
        $this->display("add");
    }else{
        $this->display("type");
    }
  	
  }
    public function edit($id=0)
    {
      if ($id>0) {
          $attributeObj = $this->attributeObj->relation(true)->find($id);
          $this->assign("attribute",$attributeObj);
          $this->display("edit");
      }else{
          $this->display("add");
      }
    }
    public function save()
    {
        $id = I('post.inputId','-1','htmlspecialchars');
        $typeId = I('post.inputTypeId','','htmlspecialchars');
        $attributezh = I('post.inputAttributezh','','htmlspecialchars');
        $mark = I('post.inputMark','','htmlspecialchars');
        $data = array();
        $data["typeId"]  = $typeId;
        $data["attributezh"] = $attributezh;
        $data["mark"] = $mark;
        $data["createtime"] = time();
        if ($this->validOrAttribute($typeId,$attributezh,$mark)) 
        {
          if($id>0)
          {
              $editResult = $this->attributeObj->where("id=%d",$id)->save($data);
              $arr = array("status"=>'1',"message"=>"编辑拓展属性成功","url"=>"index");
          }
          else
          {
              $addResult =  $this->attributeObj->add($data);
              if($addResult) {
                  $arr = array("status"=>'1',"message"=>"添加拓展属性成功","url"=>"index");
              }else{
                  $arr = array("status"=>'0',"message"=>"添加拓展属性失败");
              }
          }
           
        }else{
          //已存在相应的数据
          $arr = array("status"=>'0',"message"=>"添加/编辑拓展属性失败,数据重复!","url"=>"index");
        }
        echo json_encode($arr);
    }

    public function validAttributezh($type)
    {
      $map['typeid']=$type;
      $map['attributezh'] =  $_POST["param"];
      $result = $this->attributeObj->where($map)->select();
      if(empty($result)){
        echo "y";
      }else{
        echo "<i class='icon-warning-sign'></i> 此属性名已存在";
      }
    }
     // 检查用户名是否已存在
    public function validMark($type)
    {
      $map['typeid']=$type;
      $map['mark'] =  $_POST["param"];
      $result = $this->attributeObj->where($map)->select();
      if(empty($result)){
        echo "y";
      }else{
        echo "<i class='icon-warning-sign'></i> 此标识符已存在";
      }
    }

    public function validOrAttribute($typeid,$attributezh,$mark)
    {
       $where['attributezh']  = $attributezh;
       $where['mark']  = $mark;
       $where['_logic'] = 'or';
       $map['_complex'] = $where;
       $map['typeid']  = $typeid;
       $result = $this->attributeObj->where($map)->select();
       if(empty($result)){
          return true;
        }else{
          return false;
        }
    } 
    /*单个删除 id*/
    public function delete($id)
    {
        parent::_delete($this->attributeObj,$id);
    }
    /* 批量删除 ids*/
    public function checkDeletes($ids)
    {
      parent::_checkDeletes($this->attributeObj,$ids);
    }
    /* 单个修改当前状态status 0 1 -1*/
    public function changeStatus($id,$status)
    {
        parent::_changeStatus($this->attributeObj,$id,$status);
    }
    /*批量 修改当前状态status 0 1 -1*/
    public function checkChanges($status,$ids)
    {
      parent::_checkChanges($this->attributeObj,$status,$ids);
    }
}
?>